package algotrader.quote;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import algotrader.book.Book;
import algotrader.book.BookLine;
import algotrader.book.BookManager;

public class EnfoqueQuotesManger {
	private Socket clientSocket;
	private DataInputStream in;
	private DataOutputStream out;
	private BookManager bookManager;
	
	public EnfoqueQuotesManger() {
		try {
			clientSocket = new Socket("201.48.132.170",8090);
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());
			out.writeUTF("C:");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void requestBook(BookManager bookManager, String simbolo) {
		this.bookManager = bookManager;
		try {
			out.writeUTF("D:"+simbolo);
			bookManager.receiveBook(new Book(simbolo));
			escutaCotacoes();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void escutaCotacoes() {
		String line;
		BookLine bookLine;
		try {
			while (true) {
				if ((line = in.readUTF()) == null || line.charAt(0) == 'H')
					continue;
				String valores[] = line.split(":");
				if(valores[0]=="D") {
					if(valores[1]=="A") {
						
					}
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
